home *** CD-ROM | disk | FTP | other *** search
- Path: news.cis.nctu.edu.tw!usenet
- From: terryt@mcs.com (Terry Trippany)
- Newsgroups: comp.lang.c++
- Subject: Re: MSVC++ 4.0 __declspec( dllexport ) and templates
- Date: 2 Jan 1996 19:51:25 GMT
- Organization: STR/Baxter Labs
- Message-ID: <4cc2bt$4dd@news.cis.nctu.edu.tw>
- References: <00001a81+00008478@msn.com>
- NNTP-Posting-Host: @159.198.73.111
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- In article <00001a81+00008478@msn.com>, sboag@msn.com says...
- >
- >I am having trouble exporting template classes, because either
- MSVC++
- >4.0 is broke, or I'm broke in the head and don't understand
- >something. See the indented comment in the code below. I've
- tried
- >lots and lots of things, but nothing seems to work. Can anyone
- lend
- >a hand?
- >
- >#define DllExport __declspec( dllexport )
- >
- >template<class T>
- > // If I do this, I get a zillion errors:
- > // class DllExport templateClass
- >// if I do this, it compiles, but I can not use the
- >// DllExport class in the dll client.
- >class templateClass
- >{
- >public:
- > templateClass(T val) : x(val) { }
- > T x;
- >};
- >
- >class DllExport apiClass
- >{
- >public:
- > apiClass(int val) : a(val) { }
- > templateClass<int> a;
- >};
-
- Hello,
-
- I have had experiences in this area and unfortunately have no
- good news. The bottom line is that you can't export templatized
- classes for use in a client with 4.0. What you have to do is
- put a wrapper around the template class and export that class.
- I do suggest taking out the template in line constructor and
- just adding the implementation to the bottom of the header file.
-
- For Example:
-
- // File : tempclass.hpp
-
- template <class T>
- class templateClass
- {
- public:
- templateClass(T val);
- private:
- // provide a copy constructor
- // you must have this to initialize x!
- templateClass(const templateClass& anotherClass);
- T x;
- };
-
- template < class T >
- templateClass<T>::templateClass(T val) : x(val)
- {
-
- }
-
- template < class T >
- templateClass<T>::templateClass(const templateClass&
- anotherClass)
- {
- x = anotherClass.x; // assuming class T has a good copy
- constructor
- }
-
-
- // File apiclass.hpp
-
- // you can export and use the following class!
-
- class DllExport apiClass
- {
-
- public:
- apiClass(int val) : a(val) {};
-
- // add manipulators for private type a here
- private:
- templateClass<int> a;
- };
-
-
- There are a lot of issues here but this works. Good luck.
-
- Terry Trippany
- terryt@mcs.com
- Strategic Technology Resources
- Chicago, Il
-
-